using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq;
using Agatha.Common;
using Agatha.Common.InversionOfControl;
using Source.Common;

namespace  Source.Web.FrameWork
{
    public abstract class BasePage : System.Web.UI.Page
    {
        public abstract void SetMessageText(string text);
    }

    public abstract class BasePage<T> : BasePage, IView where T : IController
    {
        protected BasePage()
        {
            Disposer.RegisterForGuaranteedDisposal(this);
            Controller = ((ICastleContainer)IoC.Container).Resolve<T>(new { View = this });
            TextRepositoryManager = new TextRepositoryManager();
        }

        public bool IsAjaxPostBack
        {
            get
            {
                ScriptManager scriptManager =  ScriptManager.GetCurrent(this.Page);
                if(scriptManager != null)
                {
                    return scriptManager.IsInAsyncPostBack;
                }

                return false;
            }
        }

        public TextRepositoryManager TextRepositoryManager { get; private set; }

        public IDictionary State
        {
            get { return ViewState; }
        }

        public override void SetMessageText(string text)
        {
            //var olaMaster = Master as OlaMenuMaster;

            //if (olaMaster != null)
            //{
            //    olaMaster.SetMessageText(text);
            //}
            //else
            //{
            //    ((OlaPopupMaster)Master).SetMessageText(text);
            //}
        }

        protected T Controller { get; private set; }

        public override void Dispose()
        {
            IoC.Container.Release(Controller);
            Controller.Dispose();
            base.Dispose();
        }

        public void DisplayMessageBox(string message)
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "displayInformationMessageBox", "alert('" + TextRepositoryManager.TryGetValue(message) + "');", true);
            DataBind();
        }

        public void DisplayInformationalMessages(string message)
        {
            DisplayInformationalMessages(new[] { message });
        }

        public void DisplayInformationalMessages(IEnumerable<string> messages)
        {
            DisplayMessages(messages);
        }

        public void DisplayErrorMessages(string message)
        {
            DisplayErrorMessages(new[] { message });
        }

        public void DisplayErrorMessages(IEnumerable<string> messages)
        {
            DisplayMessages(messages);
        }

        public void DisplayValidationMessages(Source.Common.ValidationSummary validationSummary)
        {
            StringBuilder sb = new StringBuilder();

            foreach (ValidationResult validationResult in validationSummary.GetValidationResult())
            {
                sb.Append(TextRepositoryManager.TryGetValue(validationResult.Message));
                if(validationResult.Details.Count > 0)
                {
                    string seperator = "\n * ";
                    sb.Append(seperator);
                    IEnumerable<string> details = validationResult.Details.Select(d => TextRepositoryManager.TryGetValue(d));
                    sb.Append(string.Join(seperator, details.ToArray()));
                }
                sb.Append("\n");
            }

            SetMessageText(sb.ToString());
        }

        private void DisplayMessages(IEnumerable<string> messages) 
        {
            SetMessageText(String.Join("\n", messages.Select(m => TextRepositoryManager.TryGetValue(m)).ToArray()));
        }

        protected static int? GetSelectedId(ListControl list)
        {
            return list.SelectedValue == "-1" ? (int?)null : Convert.ToInt32(list.SelectedValue);
        }

        protected static int GetIdForCurrentRow(IButtonControl link)
        {
            return Convert.ToInt32(link.CommandArgument);
        }

        protected static void PrepareDropDownList(ListControl list, object dataSource, string textField, string valueField)
        {
            list.DataSource = dataSource;
            list.DataTextField = textField;
            list.DataValueField = valueField;
        }
    }
}